﻿/******************************************************************************
 *  作者：       scott
 *  创建时间：   2012/3/4 13:04:05
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyMvcBlog.Infrastructure;
using EasyMvcBlog.Data;
using EasyMvcBlog.Infrastructure.Repository.DataRepository;
using EasyMvcBlog.DTO;

namespace EasyMvcBlog.Domain
{
    public class BlogDomain
    {
        private static BlogRepository blogRepository = new BlogRepository();

        private static string title;
        private static string subTitle;
        private static string themeName;

        /// <summary>
        /// 博客标题
        /// </summary>
        public static string Title
        {
            get
            {
                return title ?? blogRepository.GetByKey("Title");
            }
        }

        /// <summary>
        /// 博客子标题
        /// </summary>
        public static string SubTitle
        {
            get { return subTitle ?? blogRepository.GetByKey("SubTitle"); }
        }

        /// <summary>
        /// 博客主题
        /// </summary>
        public static string ThemeName
        {
            get { return themeName ?? blogRepository.GetByKey("Theme"); }
        }

        /// <summary>
        /// 刷新博客设置，一般在更改了博客设置事件中调用
        /// </summary>
        public static void UpdateBlogSetting()
        {
            title = subTitle = themeName = null;
        }

        /// <summary>
        /// 校验密码是否正确
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static bool CheckPwd(string pwd)
        {
            return pwd.EncryptMD5() == blogRepository.GetByKey("adminPwd");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        public static bool ChangePwd(string oldPwd, string newPwd)
        {
            if (CheckPwd(oldPwd))
            {
                return blogRepository.SetKeyValue("adminPwd", newPwd.EncryptMD5());
            }
            return false;
        }

        public static bool UpdateBlogSetting(BlogDTO blog)
        {
           return  blogRepository.SetKeyValue("Title", blog.Title) && blogRepository.SetKeyValue("SubTitle", blog.SubTitle);
        }
    }
}
